﻿import numpy as np
   
# 假设有4个老虎机臂，真实奖励概率   
true_rewards = [0.1, 0.5, 0.3, 0.9]   
num_arms = len(true_rewards)   
estimates = np.zeros(num_arms)  # 估计奖励   
chosen_count = np.zeros(num_arms)  # 每个臂被选择的次数   
epsilon = 0.1   
trials = 1000   
rewards = []
   
for _ in range(trials):
    if np.random.random() < epsilon:
        # 探索：随机选择一个臂
        choice = np.random.randint(num_arms)
    else:
        # 利用：选择当前估计奖励最高的臂
        choice = np.argmax(estimates)

    # 模拟拉动臂的结果
    reward = np.random.binomial(1, true_rewards[choice])
    rewards.append(reward)
    chosen_count[choice] += 1
    
    # 更新估计值
    estimates[choice] += (reward - estimates[choice]) / chosen_count[choice]
   
print("估计的奖励概率:", estimates)   
print("真实的奖励概率:", true_rewards)   
print("总奖励:", sum(rewards))
